/**
 * 字典管理初始化
 */
var Dict = {
    id: "DictTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    ztree: null,
    layerIndex: -1,
    categoryId:0,
    categoryName:null
};

/**
 * 初始化表格的列
 */
Dict.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '名称', field: 'name', align: 'left', valign: 'middle', sortable: true},
        {title: '编码', field: 'code', align: 'left', valign: 'middle', sortable: true},
        {title: '详情', field: 'detail', align: 'left', valign: 'middle', sortable: true},
        {title: '备注', field: 'description', align: 'left', valign: 'middle', sortable: true},
        {title: '操作', field: 'id', align: 'center', width: '80px', valign: 'middle',formatter:function(value,row,index){
    		var _html = '<a href="javascript:Dict.edit(\''+row.id+'\')" title="修改"><i class="fa fa-pencil"></i></a>';
    		_html += '<a href="javascript:Dict.del(\''+row.id+'\',\''+row.name+'\')" title="删除"><i class="fa fa-trash-o"></i></a>';
    		return _html;
        }}];
};

/**
 * 检查是否选中
 */
Dict.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Sys.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Dict.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加字典
 */
Dict.add = function () {
	if(Dict.categoryId&&Dict.categoryId!=0){
		this.layerIndex = Sys.open('新增字典',Sys.backendPath + '/dict/dict_add',['800px', '420px'],function(layero, index, iframeWin){
			$(iframeWin.document).find("#categoryName").attr("value", Dict.categoryName);
			$(iframeWin.document).find("#categoryId").attr("value", Dict.categoryId);
		});
	}else{
		this.layerIndex = Sys.open('新增字典',Sys.backendPath + '/dict/dict_add',['1000px', '420px']);
	}
};

/**
 * 打开查看字典详情
 */
Dict.edit = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
    	this.layerIndex = Sys.open('修改字典',Sys.backendPath + '/dict/dict_edit/' + id,['1000px', '420px']);
    }
};

/**
 * 删除字典
 */
Dict.del = function (id,name) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
	if (flag) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/dict/delete", function (data) {
	            Sys.success("删除成功!");
	            Dict.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("dictId", id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除字典【" + name + "】?",operation);
    }
};

/**
 * 查询字典列表
 */
Dict.search = function () {
    var queryData = {};
    queryData['categoryId'] = Dict.categoryId;
    queryData['condition'] = $("#condition").val();
    Dict.table.refresh({query: queryData});
};

Dict.onClickDict = function (e, treeId, treeNode) {
    Dict.categoryId = treeNode.id;
    Dict.categoryName = treeNode.name;
    Dict.search();
};


/**
 * 点击添加字典分类
 */
Dict.openAddCategory = function () {
	this.layerIndex = Sys.open('新增分类',Sys.backendPath + '/dictcategory/to_add',['600px', '400px']);
};

/**
 * 打开查看字典分类详情
 */
Dict.openCategoryDetail = function () {
    if (Dict.categoryId) {
    	this.layerIndex = Sys.open('修改分类',Sys.backendPath + '/dictcategory/to_update/' + Dict.categoryId,['600px', '400px']);
    }else{
        Sys.info("请先选中某一分类！");
    }
};

/**
 * 删除字典分类
 */
Dict.delCategory = function () {
    if (Dict.categoryId) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/dictcategory/delete", function (data) {
	            Sys.success("删除成功!");
	            Dict.ztree.init();
	            Dict.categoryId=null;
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("dictCategoryId",Dict.categoryId);
	        ajax.start();
	    };
	    Sys.confirm("是否删除字典分类?",operation);
    }else{
        Sys.info("请先选中某一分类！");
    }
};
$(function () {
    var defaultColunms = Dict.initColumn();
    var table = new BSTable(Dict.id, Sys.backendPath + "/dict/list", defaultColunms);
    table.setPaginationType("client");
    table.setSubHeight(-5);
    table.setSort("weight");
    Dict.table = table.init();
    var ztree = new $ZTree("categoryTree", Sys.backendPath + "/dictcategory/tree");
    ztree.bindOnClick(Dict.onClickDict);
    ztree.init();
    Dict.ztree = ztree;
    fuzzySearch('categoryTree','#seachKey',null,true);
});
